//
//  WriteOrderViewController.m
//  XDLookPic
//
//  Created by wanglong8889@126.com on 14-5-25.
//  Copyright (c) 2014年 XD-XY. All rights reserved.
//

#import "WriteOrderViewController.h"
#import "QingdanViewController.h"
//#import "OrderSucessViewController.h"
//#import "AddressCell.h"
//#import "ReceiverListViewController.h"

#import "Address.h"
#import "ThirdViewController.h"
#import "AddressCell.h"
#import "OrderSucessViewController.h"
#define CHANGEY    115
@interface WriteOrderViewController ()
{
    UIView * chooseAd_BgView;
    UIImageView * time_BgView;
    NSMutableArray * dataArray;
    BOOL isScrollTop;
    
}
@end

@implementation WriteOrderViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.titleLabel.text = @"填写订单";
    [self.leftBtn setBackgroundImage:[UIImage imageNamed:@"pd_25_bai"] forState:UIControlStateNormal];
    self.leftBtn.hidden = NO;
    
    self.contentView.backgroundColor = RGBA(230, 230, 230, 1.0);
    if (IOS7) {
        self.navigationBarBg.image = [UIImage imageNamed:@"redIOS7"];
    }else{
        self.navigationBarBg.image = [UIImage imageNamed:@"redIOS6"];
    }
    dataArray = [[NSMutableArray alloc] init];
    [dataArray addObject:@{@"name":@"工作日和周末皆可",@"status":@"1"}];
    [dataArray addObject:@{@"name":@"仅工作日送货",@"status":@"0"}];
    [dataArray addObject:@{@"name":@"仅周末送货",@"status":@"0"}];
    
    self.titleLabel.textColor = [UIColor whiteColor];
    self.myAddress = [[Address alloc] init];
    [self initWriteOrderView];
    
}
//界面布局
-(void)initWriteOrderView{
    
    
    scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height)];
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.delegate = self;
    scrollView.contentSize = CGSizeMake(0, self.contentView.frame.size.height+50);
    [self.contentView addSubview:scrollView];
    
    //收件人布局
    shoujiaView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 300, 84)];
    shoujiaView.image = [UIImage imageNamed:@"wumorenback"];
    shoujiaView.userInteractionEnabled = YES;
    
    nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(13, 18,65, 18)];
    nameLabel.backgroundColor = [UIColor clearColor];
    nameLabel.font = [UIFont systemFontOfSize:14.0];
    
    [shoujiaView addSubview:nameLabel];
    
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fillOutReceiver)];
    [shoujiaView addGestureRecognizer:tap];
    
    
    
    phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(97+20, 18,145, 18)];
    phoneLabel.backgroundColor = [UIColor clearColor];
    phoneLabel.font = [UIFont systemFontOfSize:14.0];
    
    [shoujiaView addSubview:phoneLabel];
    
    
    
    
    addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(nameLabel.frame.origin.x, nameLabel.frame.origin.y+nameLabel.frame.size.height+5,280, 30)];
    addressLabel.numberOfLines = 0;
    addressLabel.backgroundColor = [UIColor clearColor];
    addressLabel.font = [UIFont systemFontOfSize:12.0];
    
    [shoujiaView addSubview:addressLabel];
    
    [scrollView addSubview:shoujiaView];
    
    for (UIView *vo in shoujiaView.subviews) {
        vo.hidden = YES;
    }
    
    
    //支付方式
    
    zhifuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    zhifuBtn.frame = CGRectMake(23/2.0, shoujiaView.frame.origin.y+shoujiaView.frame.size.height+10, 297, 47);
    
    [zhifuBtn setBackgroundImage:[UIImage imageNamed:@"write_liuyan"] forState:
     UIControlStateNormal];
    
    [zhifuBtn setBackgroundImage:[UIImage imageNamed:@"write_liuyan"] forState:UIControlStateHighlighted];
    
//    [zhifuBtn addTarget:self action:@selector(goTochangeZhifu) forControlEvents:UIControlEventTouchUpInside];
    
    
    UILabel *zhifuLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 12, 60, 23)];
    zhifuLabel.backgroundColor = [UIColor clearColor];
    zhifuLabel.text = @"支付方式";
    zhifuLabel.font = [UIFont systemFontOfSize:14.0];
    
    [zhifuBtn addSubview:zhifuLabel];
    
    
    
    UILabel *znameLabel = [[UILabel alloc] initWithFrame:CGRectMake(zhifuBtn.frame.size.width-70, 12, 60, 23)];
    znameLabel.backgroundColor = [UIColor clearColor];
    znameLabel.text = @"在线支付";
    znameLabel.textAlignment = NSTextAlignmentRight;
    znameLabel.textColor = [UIColor grayColor];
    znameLabel.font = [UIFont systemFontOfSize:14.0];
    
    [zhifuBtn addSubview:znameLabel];
    
    
    
    
    [scrollView addSubview:zhifuBtn];
    
    
    
    //配送方式
    
    peisongBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    peisongBtn.frame = CGRectMake(23/2.0, zhifuBtn.frame.origin.y+zhifuBtn.frame.size.height+10, 297, 47);
    
    [peisongBtn setBackgroundImage:[UIImage imageNamed:@"write_liuyan"] forState:
     UIControlStateNormal];
    
    [peisongBtn setBackgroundImage:[UIImage imageNamed:@"write_liuyan"] forState:UIControlStateHighlighted];
    
    //    [peisongBtn addTarget:self action:@selector(goTochangeZhifu) forControlEvents:UIControlEventTouchUpInside];
    
    
    UILabel *peisongLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 12, 60, 23)];
    peisongLabel.backgroundColor = [UIColor clearColor];
    peisongLabel.text = @"配送方式";
    peisongLabel.font = [UIFont systemFontOfSize:14.0];
    
    [peisongBtn addSubview:peisongLabel];
    
    
    
    UILabel *pnameLabel = [[UILabel alloc] initWithFrame:CGRectMake(zhifuBtn.frame.size.width-70, 12, 60, 23)];
    pnameLabel.backgroundColor = [UIColor clearColor];
    pnameLabel.text = @"快递运输";
    pnameLabel.textAlignment = NSTextAlignmentRight;
    pnameLabel.textColor = [UIColor grayColor];
    pnameLabel.font = [UIFont systemFontOfSize:14.0];
    
    [peisongBtn addSubview:pnameLabel];
    
    
    
    
    [scrollView addSubview:peisongBtn];
    
    
    
    //费用 ，送货时间
    peisongView = [[UIImageView alloc] initWithFrame:CGRectMake(23/2.0, peisongBtn.frame.origin.y+peisongBtn.frame.size.height+10, 297, 107)];
    peisongView.userInteractionEnabled = YES;
    peisongView.image = [[UIImage imageNamed:@"write_liuyan"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
    
    
    //商品金额
    UILabel *fangshiLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 6, 70, 24)];
    fangshiLabel.backgroundColor = [UIColor clearColor];
    fangshiLabel.text = @"商品金额";
    fangshiLabel.font = [UIFont systemFontOfSize:15.0];
    
    [peisongView addSubview:fangshiLabel];
    
    
    //商品金额
    UILabel *kuaidiLabel = [[UILabel alloc] initWithFrame:CGRectMake(138, 8, 140, 20)];
    kuaidiLabel.backgroundColor = [UIColor clearColor];
    kuaidiLabel.textColor = [UIColor redColor];
    kuaidiLabel.text = [NSString stringWithFormat:@"￥%.2f",self.totalAmount];
    kuaidiLabel.textAlignment = NSTextAlignmentRight;
    kuaidiLabel.font = [UIFont systemFontOfSize:14.0];
    
    [peisongView addSubview:kuaidiLabel];
    
    
    UILabel *kNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(fangshiLabel.frame.origin.x,45, 70, 20)];
    kNameLabel.backgroundColor = [UIColor clearColor];
    kNameLabel.text = @"快递运费:";
    kNameLabel.font = [UIFont systemFontOfSize:14.0];
    
    [peisongView addSubview:kNameLabel];
    
    
    kuaiPriceLabel = [[UILabel alloc] initWithFrame:CGRectMake(peisongView.frame.size.width-100,45, 80, 20)];
    kuaiPriceLabel.backgroundColor = [UIColor clearColor];
    kuaiPriceLabel.text = @"￥0.00";
    kuaiPriceLabel.textColor = [UIColor redColor];
    kuaiPriceLabel.textAlignment = NSTextAlignmentRight;
    kuaiPriceLabel.font = [UIFont systemFontOfSize:14.0];
    
    [peisongView addSubview:kuaiPriceLabel];
    
    
    UILabel *dLabel = [[UILabel alloc] initWithFrame:CGRectMake(fangshiLabel.frame.origin.x,kNameLabel.frame.origin.y+kNameLabel.frame.size.height+12,65, 20)];
    dLabel.backgroundColor = [UIColor clearColor];
    dLabel.text = @"送货时间:";
    dLabel.font = [UIFont systemFontOfSize:14.0];
    
    [peisongView addSubview:dLabel];
    
    
    songTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(fangshiLabel.frame.origin.x+65,kNameLabel.frame.origin.y+kNameLabel.frame.size.height+12, 230, 20)];
    songTimeLabel.backgroundColor = [UIColor clearColor];

    songTimeLabel.text = @"工作日和周末皆可";
//    songTimeLabel.textColor = [UIColor grayColor];
    songTimeLabel.font = [UIFont systemFontOfSize:14.0];
    
    [peisongView addSubview:songTimeLabel];
    
    
    [scrollView addSubview:peisongView];
    
    

    UIImageView *arrowView0 = [[UIImageView alloc] initWithFrame:CGRectMake(271, (peisongView.frame.size.height-12.5)/2.0f+18+8, 8, 25/2.0)];
    arrowView0.image = [UIImage imageNamed:@"write_arrow"];
    [peisongView addSubview:arrowView0];

    UIButton * bbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    bbtn.frame = CGRectMake(fangshiLabel.frame.origin.x, kNameLabel.frame.origin.y, 280, 60);
    bbtn.backgroundColor = [UIColor clearColor];
    [bbtn addTarget:self action:@selector(chooseTime) forControlEvents:UIControlEventTouchUpInside];
    [peisongView addSubview:bbtn];
    
    //商品清单
    dingdanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    dingdanBtn.frame = CGRectMake(peisongView.frame.origin.x, peisongView.frame.origin.y+peisongView.frame.size.height+10, peisongView.frame.size.width, 47);
    
    [dingdanBtn setBackgroundImage:[UIImage imageNamed:@"write_liuyan"] forState:
     UIControlStateNormal];
    
    [dingdanBtn setBackgroundImage:[UIImage imageNamed:@"write_liuyan"] forState:UIControlStateHighlighted];
    
    [dingdanBtn addTarget:self action:@selector(goToQingDan) forControlEvents:UIControlEventTouchUpInside];
    
    
    UILabel *qingdanLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 12, 60, 23)];
    qingdanLabel.backgroundColor = [UIColor clearColor];
    qingdanLabel.text = @"商品清单";
    qingdanLabel.font = [UIFont systemFontOfSize:14.0];
    
    [dingdanBtn addSubview:qingdanLabel];
    
    
    UIImageView *arrowView = [[UIImageView alloc] initWithFrame:CGRectMake(274, 18, 8, 25/2.0)];
    arrowView.image = [UIImage imageNamed:@"write_arrow"];
    [dingdanBtn addSubview:arrowView];
    
    [scrollView addSubview:dingdanBtn];
    
    
    //留言
    
    liuyanView = [[UIImageView alloc] initWithFrame:CGRectMake(dingdanBtn.frame.origin.x, dingdanBtn.frame.origin.y+dingdanBtn.frame.size.height+10, dingdanBtn.frame.size.width, 47)];
    liuyanView.image = [[UIImage imageNamed:@"write_liuyan"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 20, 10, 20)];
    liuyanView.userInteractionEnabled = YES;
    [scrollView addSubview:liuyanView];
    
    
    liuyanTf = [[UITextField alloc] initWithFrame:CGRectMake(5, 5, 287, 37)];
    liuyanTf.placeholder = @"留言，最多200字";
    liuyanTf.delegate = self;
    liuyanTf.returnKeyType = UIReturnKeyDone;
    liuyanTf.font = [UIFont systemFontOfSize:12.0];
    [liuyanTf addTarget:self action:@selector(textischange) forControlEvents:UIControlEventEditingChanged];
    [liuyanView addSubview:liuyanTf];
    
    
    
    
    //初始化下面结算view
    UIView *jiesuanView = [[UIView alloc] initWithFrame:CGRectMake(0, self.contentView.frame.size.height-40, UI_SCREEN_WIDTH, 40)];
    jiesuanView.backgroundColor = RGBA(87, 87, 87, 1.0);
    
    
    [self.contentView addSubview:jiesuanView];

    
    
    
    totalLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 8, 150, 24)];
    totalLabel.backgroundColor = [UIColor clearColor];
    totalLabel.textColor = [UIColor whiteColor];
    totalLabel.text = @"应付总额:  ￥323";
    totalLabel.font = [UIFont systemFontOfSize:14];
    [jiesuanView addSubview: totalLabel];
    
    
    UIButton *jiesuanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [jiesuanBtn setTitle:@"提交订单" forState:UIControlStateNormal];
    [jiesuanBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    jiesuanBtn.titleLabel.font = [UIFont systemFontOfSize:12.5];
    [jiesuanBtn setBackgroundImage:[UIImage imageNamed:@"write_tijiao"] forState:UIControlStateNormal];
    jiesuanBtn.frame = CGRectMake(233, 7, 71, 26);
    [jiesuanBtn addTarget:self action:@selector(commitOrder) forControlEvents:UIControlEventTouchUpInside];
    
    [jiesuanView addSubview:jiesuanBtn];
    
    
    [self createChooseTimeTypeViews];
    
    [self checkIsMoRen];
    
   
}

-(void)createChooseTimeTypeViews
{
    chooseAd_BgView = [[UIView alloc] initWithFrame:self.view.bounds];
    chooseAd_BgView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:chooseAd_BgView];
    
    if (iPhone5){
        time_BgView = [[UIImageView alloc] initWithFrame:CGRectMake(23, height_y(self.navigationBarView)+26, 548/2.0f, 577/2.0f)];
    }else{
        time_BgView = [[UIImageView alloc] initWithFrame:CGRectMake(23, height_y(self.navigationBarView)+10, 548/2.0f, 577/2.0f)];
    }
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, time_BgView.frame.size.width, 54)];
    label.text = @"请选择";
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:16.5];
    [time_BgView addSubview:label];
    label.backgroundColor = [UIColor clearColor];
    
    UIButton * closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    closeBtn.frame = CGRectMake(225, 13.5, 34, 34);
    [closeBtn setBackgroundImage:[UIImage imageNamed:@"closeimg"] forState:UIControlStateNormal];
    [closeBtn addTarget:self action:@selector(closeChooseCity) forControlEvents:UIControlEventTouchDown];
    [time_BgView addSubview:closeBtn];
    
    time_BgView.image = [UIImage imageNamed:@"chooseaddressbgimg2"];
    time_BgView.userInteractionEnabled = YES;
    [chooseAd_BgView addSubview:time_BgView];
    
    timeTableView = [[UITableView alloc] initWithFrame:CGRectMake(33-23,54+9, 254, 274-(225/2.0f)) style:UITableViewStylePlain];
    timeTableView.backgroundColor = [UIColor clearColor];
    timeTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    timeTableView.delegate = self;
    timeTableView.dataSource = self;
    [time_BgView addSubview:timeTableView];
    
    UIButton * nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    nextBtn.frame = CGRectMake((time_BgView.frame.size.width-66)/2.0f,height_y(timeTableView)+15,66,26);
    [nextBtn setTitle:@"确定" forState:UIControlStateNormal];
    nextBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [nextBtn setBackgroundImage:[UIImage imageNamed:@"next_nor_img"] forState:UIControlStateNormal];
    [nextBtn setBackgroundImage:[UIImage imageNamed:@"next_sel_img"] forState:UIControlStateHighlighted];
    [nextBtn addTarget:self action:@selector(nextBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [time_BgView addSubview:nextBtn];
    
    [chooseAd_BgView setHidden:YES];
}

-(void)nextBtnClick
{
    [chooseAd_BgView setHidden:YES];
    for (NSDictionary * dict in dataArray){
        if ([dict[@"status"] isEqualToString:@"1"]){
            songTimeLabel.text = [NSString stringWithFormat:@"%@",dict[@"name"]];
        }
    }
}

-(void)closeChooseCity
{
    [chooseAd_BgView setHidden:YES];
}

#pragma mark UITableView Delegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 54;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [dataArray count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellName = @"cell";
    AddressCell * cell = [tableView dequeueReusableCellWithIdentifier:cellName];
    if (cell == nil){
        cell = [[AddressCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName];
    }
    NSDictionary * dict = [dataArray objectAtIndex:indexPath.row];
    if([dict[@"status"] isEqualToString:@"0"]){
        [cell.selectBtn setBackgroundImage:[UIImage imageNamed:@"choose_no_img"] forState:UIControlStateDisabled];
    }else{
        [cell.selectBtn setBackgroundImage:[UIImage imageNamed:@"choose_yes_img"] forState:UIControlStateDisabled];
    }
    cell.titleLB.text = dict[@"name"];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    for (int i = 0 ;i <[dataArray count];i++){
        NSDictionary * dict = [dataArray objectAtIndex:i];
        if (i == indexPath.row){
            NSMutableDictionary * mdict = [[NSMutableDictionary alloc] initWithDictionary:dict];
            [mdict setValue:@"1" forKey:@"status"];
            [dataArray replaceObjectAtIndex:i  withObject:mdict];
        }else{
            NSMutableDictionary * mdict = [[NSMutableDictionary alloc] initWithDictionary:dict];
            [mdict setValue:@"0" forKey:@"status"];
            [dataArray replaceObjectAtIndex:i  withObject:mdict];
        }
    }
    [timeTableView reloadData];
    
//    [self nextBtnClick];
}

//选择送货时间
-(void)chooseTime
{
    chooseAd_BgView.transform = CGAffineTransformMakeScale(0, 0);
    [UIView animateWithDuration:0.2 animations:^{
        chooseAd_BgView.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.1 animations:^{
            chooseAd_BgView.transform=CGAffineTransformScale(chooseAd_BgView.transform, 1.1, 1.1);
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.1 animations:^{
                chooseAd_BgView.transform = CGAffineTransformIdentity;
            } completion:^(BOOL finished) {
                
            }];
        }];
    }];
    chooseAd_BgView.hidden =NO;
}


//查看 商品清单
-(void)goToQingDan{
    //跳到下一级列表页
    QingdanViewController * list = [[QingdanViewController alloc] init];

    list.dataArray = [NSMutableArray arrayWithArray:self.dataArr];
    
    [self.navigationController pushViewController:list animated:YES];
}
//提交订单

-(void)commitOrder{
    if (!IS_NOT_EMPTY(nameLabel.text)) {
        [XDTools showTips:@"请先选择收件人地址" toView:self.contentView];
    }else{
        [self commitDingDanDatas];
    }
    
    
}

#pragma mark - 去收货人列表选择收货人
-(void)fillOutReceiver
{
//    ReceiverListViewController * receiverVC = [[ReceiverListViewController alloc] init];
//    receiverVC.accessVC = mWriteOrderVC;
//    receiverVC.writeVC = self;
//    [self.navigationController pushViewController:receiverVC animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



////////////////代理

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    float yy = 0.0;
    if (iPhone5) {
        yy = 180;
    }else{
        yy = 260;
    }
    [UIView animateWithDuration:.3 animations:^{
        scrollView.contentOffset = CGPointMake(0, yy);
        isScrollTop = YES;
    }];
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self cancelLiuYan];
    return YES;
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
   
    if (isScrollTop) {
        [liuyanTf resignFirstResponder];
        
    }
    
    
}
-(void)cancelLiuYan{
    [liuyanTf resignFirstResponder];
    [UIView animateWithDuration:.3 animations:^{
        scrollView.contentOffset = CGPointMake(0, 0);
        isScrollTop = NO;
    }];
}

-(void)textischange{
    DDLOG(@"range = %d",[liuyanTf.text length]);
}




//提交订单

- (void)commitDingDanDatas{
    
    
    NSString * uid = [[[NSUserDefaults standardUserDefaults] objectForKey:@"MYSELF_INFO"] objectForKey:@"uid"];
    
    uid = @"1281019";///假数据
    NSString *liuyan;
    if (!IS_NOT_EMPTY(liuyanTf.text)) {
        liuyan = @"";
    }else{
        liuyan = liuyanTf.text;
    }
    NSString *orderAmount = [NSString stringWithFormat:@"%.2f",self.totalAmount];
    if ([XDTools NetworkReachable])
    {
        NSDictionary * dic;
        NSString *rId;
        if ([self.type isEqualToString:@"1"]) {
            rId = _receiveId;
            
        }else{
            rId = _shopId;
           
        }
        dic = @{@"recID":[[rId base64] base64],
                @"userID":[[uid base64] base64],
                @"addressID":[[self.myAddress.addressId base64] base64],
                @"order_amount":[[orderAmount base64] base64],
                @"shipping_fee":[[[NSString stringWithFormat:@"%@",kuaidiFei] base64] base64],
                @"postscript":[[liuyan base64] base64],
                @"best_time":[[songTimeLabel.text base64] base64],
                @"version":@"1.0"
                };
        
        
        ASIHTTPRequest * request = [XDTools postECShopRequestWithDict:dic API:API_ADDORDER sign:@"1"];
        __weak ASIHTTPRequest * mrequest = request;
        [request setCompletionBlock:^{
            
            [XDTools hideProgress:self.view];
            
            NSDictionary *tempDic = [XDTools  JSonFromString:[mrequest responseString]];
            DDLOG(@"temdic = %@",tempDic);
            if ([[tempDic objectForKey:@"status"] intValue] == 1)
            {
                
                OrderSucessViewController *orderVC = [[OrderSucessViewController alloc] init];
                orderVC.orderId = [[tempDic objectForKey:@"data"] objectForKey:@"order_sn"];
                orderVC.totalMo = zonge;
                orderVC.goodName = [[tempDic objectForKey:@"data"] objectForKey:@"goods_name"];
                [self.navigationController pushViewController:orderVC animated:YES];
                
            }else{
                [XDTools showTips:[tempDic objectForKey:@"msg"] toView:self.view];
            }
            
            
        }];
        
        [request setFailedBlock:^{
            [XDTools hideProgress:self.view];
            NSError *error = [mrequest error];
            DDLOG_CURRENT_METHOD;
            DDLOG(@"error=%@",error);
            if (mrequest.error.code == 2) {
                [XDTools showTips:@"网络请求超时" toView:self.view];
            }
        }];
        [XDTools showProgress:self.view];
        [request startAsynchronous];
        
    }
    else
    {
        [XDTools showTips:brokenNetwork toView:self.view];
    }
}


- (void)backPrePage
{
    [self.shopVC getShopTrolleyListDatas];
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)checkIsMoRen{
    NSString * uid = [[[NSUserDefaults standardUserDefaults] objectForKey:@"MYSELF_INFO"] objectForKey:@"uid"];
    uid = @"1281019";///假数据
   
    if ([XDTools NetworkReachable])
    {
        NSDictionary * dic = @{@"userID":uid
                               };
        
        ASIFormDataRequest *request = [XDTools postECShopRequestWithDict:dic API:API_GETDEFAULTADDRESS sign:@"2"];
        __weak ASIFormDataRequest * mrequest = request;
        [request setCompletionBlock:^{
            
            [XDTools hideProgress:self.view];
            
            NSDictionary *tempDic = [XDTools  JSonFromString:[mrequest responseString]];
            DDLOG(@"temdic = %@",tempDic);
            if ([[tempDic objectForKey:@"status"] intValue] == 1)
            {
                NSDictionary *dic = [tempDic objectForKey:@"data"];
                if ([dic count] == 0) {
                    [self noMorendizhi];
                }else{
                    [self haveMorendizhi];
                    self.myAddress.name = [dic objectForKey:@"username"];
                    self.myAddress.phone = [dic objectForKey:@"mobile"];
                    self.myAddress.detail = [dic objectForKey:@"address"];
                    self.myAddress.provinceId = [dic objectForKey:@"province"];
                    self.myAddress.addressId = [dic objectForKey:@"address_id"];
                    
                    [self getShoppingFee:self.myAddress.provinceId];
                    [self refreshInfos];
                    
                }
                
            }else{
                [XDTools showTips:[tempDic objectForKey:@"msg"] toView:self.view];
            }
            
            
        }];
        
        [request setFailedBlock:^{
            [XDTools hideProgress:self.view];
            NSError *error = [mrequest error];
            DDLOG_CURRENT_METHOD;
            DDLOG(@"error=%@",error);
            if (mrequest.error.code == 2) {
                [XDTools showTips:@"网络请求超时" toView:self.view];
            }
        }];
        
        [request startAsynchronous];
        
    }
    else
    {
        [XDTools showTips:brokenNetwork toView:self.view];
    }
}

-(void)noMorendizhi{
    for (UIView *vo in shoujiaView.subviews) {
        vo.hidden = YES;
    }
    shoujiaView.image = [UIImage imageNamed:@"wumorenback"];
}

-(void)haveMorendizhi{
    for (UIView *vo in shoujiaView.subviews) {
        vo.hidden = NO;
    }
    shoujiaView.image = [[UIImage imageNamed:@"wirte_shou_back"] resizableImageWithCapInsets:UIEdgeInsetsMake(60,20, 20, 100)];
}

-(void)refreshInfos{
    nameLabel.text = self.myAddress.name;
    phoneLabel.text = self.myAddress.phone;
    addressLabel.text = self.myAddress.detail;
    
     shoujiaView.image = [[UIImage imageNamed:@"wirte_shou_back"] resizableImageWithCapInsets:UIEdgeInsetsMake(60,20, 20, 100)];
    [self haveMorendizhi];
    
     CGRect rect = [XDTools xyAutoSizeOfWidget:addressLabel andSize:CGSizeMake(280, 1000) andtextFont:12.0];
    addressLabel.frame = rect;
    
    CGRect shoujiaRect = shoujiaView.frame;
    shoujiaRect.size.height = nameLabel.frame.origin.y+nameLabel.frame.size.height+20+rect.size.height;
    
    shoujiaView.frame = shoujiaRect;
    
     zhifuBtn.frame = CGRectMake(23/2.0, shoujiaView.frame.origin.y+shoujiaView.frame.size.height+10, 297, 47);
    
    peisongBtn.frame = CGRectMake(23/2.0, zhifuBtn.frame.origin.y+zhifuBtn.frame.size.height+10, 297, 47);
    
    peisongView.frame = CGRectMake(23/2.0, peisongBtn.frame.origin.y+peisongBtn.frame.size.height+10, 297, 107);
    
    dingdanBtn.frame = CGRectMake(peisongView.frame.origin.x, peisongView.frame.origin.y+peisongView.frame.size.height+10, peisongView.frame.size.width, 47);
    liuyanView.frame = CGRectMake(dingdanBtn.frame.origin.x, dingdanBtn.frame.origin.y+dingdanBtn.frame.size.height+10, dingdanBtn.frame.size.width, 47);
    
    
    [self getShoppingFee:self.myAddress.provinceId];
    

    
}


-(void)getShoppingFee:(NSString *)provinceId{
    NSString *reId;
    if ([self.type isEqualToString:@"1"]) {
        reId = _receiveId;
    }else{
        reId = self.shopId;
    }
    
    NSDictionary * dict = @{@"recID": reId,
                            @"provinceID":provinceId};
    
    ASIHTTPRequest *request = [XDTools postECShopRequestWithDict:dict API:API_GETKUAIDI sign:@"1"];
    __weak ASIHTTPRequest * mrequest = request;
    
    [request setCompletionBlock:^{
        [XDTools hideProgress:self.view];
        
        NSDictionary *tempDic = [XDTools  JSonFromString:[mrequest responseString]];
        DDLOG(@"DELPPP::%@",tempDic);
        if([[tempDic objectForKey:@"status"]intValue] == 1){
            
            kuaidiFei = [[tempDic objectForKey:@"data"] objectForKey:@"shipping_fee"];
            NSString * kuaidifeiStr = [NSString stringWithFormat:@"%.2f",kuaidiFei.floatValue];
            kuaiPriceLabel.text = [NSString stringWithFormat:@"￥%@",kuaidifeiStr];
            
            zonge = [NSString stringWithFormat:@"%.2f",[kuaidiFei floatValue]+self.totalAmount];
            totalLabel.text = [NSString stringWithFormat:@"应付总额:  ￥%@",zonge];
            
        }else{
            [XDTools showTips:tempDic[@"msg"] toView:self.view];
        }
        
    }];
    
    [request setFailedBlock:^{
        
        [XDTools hideProgress:self.view];
        NSError *error = [mrequest error];
        DDLOG(@"error=%@",error);
        if (mrequest.error.code == 2) {
            [XDTools showTips:@"网络请求超时" toView:self.view];
        }
    }];
    
    [XDTools showProgress:self.view];
    [request startAsynchronous];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
